package com.dcm.seckill.controller;

import com.dcm.common.annotation.EnableResponseBodyAdvice;
import com.dcm.common.annotation.IgnoreResponseBodyAdvice;
import com.dcm.common.vo.CommonResponse;
import com.dcm.seckill.cilent.ItemClient;
import com.dcm.seckill.entity.SeckillShopEntity;
import com.dcm.seckill.service.SeckillShopService;
import com.dcm.seckill.vo.ItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 牛浩
 */
@Api(tags = "seckillShop")
@RestController
@RequestMapping("/seckillShop")
@CrossOrigin
@EnableResponseBodyAdvice
public class SeckillShopController {
    @Autowired
    private SeckillShopService miaoOrderService;
    @Autowired
    private ItemClient itemCilent;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public List<SeckillShopEntity> findAll(){
        return miaoOrderService.findAll();
    }

    /**
     * 远程调用item查询所有方法
     * @return
     */
    @ApiOperation("远程调用item查询所有方法")
    @GetMapping("/findItemAll")
    public CommonResponse findItemAll(){
        return itemCilent.findAll();
    }

    /**
     * 远程调用item根据id查询
     * @param id
     * @return
     */
    @ApiOperation("远程调用item根据id查询")
    @GetMapping("/findItemById")
    public CommonResponse<ItemVo> findItemById(@RequestParam(value = "id",required = false) Long id){
        return itemCilent.findItemById(id);
    }

    /**
     * 添加
     * @param miaoOrder
     */
    @Autowired
    private StringRedisTemplate redisTemplate;
    @ApiOperation("添加")
    @PostMapping("/add")
    public void add(@RequestBody SeckillShopEntity seckillShop){
        //初始化键值对
        redisTemplate.opsForValue().set(seckillShop.getId()+"",seckillShop.getLimitedSurplus()+"");
        miaoOrderService.add(seckillShop);
    }

    /**
     * 远程调用item模糊查询
     * @param specId
     * @param id
     * @param itemName
     * @return
     */
    @ApiOperation("远程调用item模糊查询")
    @GetMapping("/searchItem")
    public CommonResponse searchItem(@RequestParam(value = "specId",required = false) Integer specId,
                                     @RequestParam(value = "id",required = false) Long id,
                                     @RequestParam(value = "itemName",required = false) String itemName){
        return itemCilent.searchItem(specId,id,itemName);
    }

    /**
     * 模糊查询
     * @param state
     * @param configure
     * @param id
     * @return
     */
    @ApiOperation("模糊查询")
    @GetMapping("/search")
    public List<SeckillShopEntity> search(@RequestParam(value = "state",required = false)Integer state,
                                          @RequestParam(value = "configure",required = false)String configure,
                                          @RequestParam(value = "id",required = false)Long id){
        return miaoOrderService.search(state,configure,id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation("根据id查询")
    @GetMapping("/findOne")
    public SeckillShopEntity findOne(@RequestParam(value = "id",required = false)Long id){
        return miaoOrderService.findOne(id);
    }

}